var dataSourceConfigApp = angular.module('dataSourceConfigApp', ['base']);
dataSourceConfigApp.controller('dataSourceConfigCtrl', ['$rootScope', '$scope', 'dataSourceConfigService', function ($rootScope, $scope, dataSourceConfigService) {
    $scope.queryFilter = {};
    $scope.typeMap = [{type: 'MySQL', name: "MySQL"}, {type: 'Oracle', name: "Oracle"}];
    //添加用户,1为添加，0为修改
    $scope.add = function (opt) {
        if(opt === 1){
            var selectArray = $("#data_list tbody input:checked");
            if (!selectArray || selectArray.length !== 1) {
                alertDialog("请选择一个");
                return;
            }
            var code = $(selectArray[0]).val();
        }
        var titleName = opt === 1 ? '修改' : '添加';
        if (code && opt === 1) {
            dataSourceConfigService.detail(code).then(function (response) {
                var type = response.data.type;
                $scope.record = response.data;
                $scope.record.type = type;
            });
        } else {
            $scope.record = {
                type: 'MySQL'
            };
        }
        layer.open({
            type: 1,
            title: titleName,
            maxmin: true,
            shadeClose: true, //点击遮罩关闭层
            area: ['576px', 'auto'],
            content: $('#edit_panel'),
            btn: ['保存', '取消'],
            yes: function (index, layero) {
                if ($("input[name=username]").val() === "") {
                    alertDialog("登录用户名不能为空");
                    return;
                }
                if ($("input[name=password]").val() === "") {
                    alertDialog("密码不能为空");
                    return;
                }
                /*if ($("#select_status").val() == "") {
                    alertDialog("用户状态能为空");
                    return;
                } else */{
                    var record = angular.copy($scope.record);
                    if (!record) {
                        layer.msg('用户不能为空！', {
                            time: 1000,
                            icon: 1
                        });
                        return;
                    }
                    //var typeStr = record.type;
                    //record.type = parseInt(lockedStr);
                    if (!code) {
                        dataSourceConfigService.add(record).then(function (response) {
                            layer.alert(response.msg, {
                                title: '提示框',
                                icon: 1,
                            }, function () {
                                layer.close(index);
                                window.location.reload();
                            });
                        });
                    } else {
                        dataSourceConfigService.edit(record).then(function (response) {
                            layer.alert('修改成功！', {
                                title: '提示框',
                                icon: 1,
                            }, function () {
                                window.location.reload();
                            });
                            layer.close(index);
                        }, function (response) {

                        });
                    }
                }
            }
        });
    }
    //删除用户
    $scope.delete = function () {
        var selectArray = $("#data_list tbody input:checked");
        if (!selectArray || selectArray.length === 0) {
            alertDialog("请选择用户");
            return;
        }
        var userIds = [];
        $.each(selectArray, function (i, e) {
            var val = $(this).val();
            userIds.push(val);
        });
        if (userIds.lenght == 0) {
            return;
        }
        layer.confirm('是否删除用户？', {
            btn: ['确定', '取消']
        }, function () {
            dataSourceConfigService.delete(userIds).then(function (resp) {
                layer.msg(resp.msg, {
                    time: 1000,
                    icon: 1
                }, function () {
                    window.location.reload();
                });
            });
        });
    };
    $scope.selectAll = function ($event) {
        var target = $event.target;
        if ($(target).prop("checked")) {
            $(".data-checkbox").each(function (i, e) {
                $(this).attr("checked", true);
            });
        } else {
            $(".data-checkbox").each(function (i, e) {
                $(this).attr("checked", false);
            });
        }
    }
}]);